import { set } from 'lodash-es'
import { defineStore } from 'pinia'
import { data } from './test'
import { listToTree } from './modules/helper'

type IUserInfo = {
    iamToken: string
    myAccountVo: any
    iamAppMenuVoList?: any[]
    appRoles?: any[]
    userFlag?: string
    userStep?: string
}

type IMenu = {
    path: string
    name: string
    component: string
    alwaysShow: boolean
    meta: {
        title: string
        icon: string
        noCache: boolean
        link: null
    }
    children: any[]
}

const businessTypeEnum = {
    1: {
        label: '联合运营平台',
        path: '/joint',
        remark: '联合运营',
    },
    2: {
        label: '武汉新房',
        path: '/new-house',
        remark: '新房业务',
    },

} as const

export const useUserStore = defineStore('user', () => {
    const userInfo = ref<IUserInfo>({
        iamToken: '',
        userFlag: '',
        userStep: '',
        iamAppMenuVoList: [],
        myAccountVo: {},
    })

    console.log('🚀 ~userInfo data:', data)

    // 开通app角色
    const appRoles = ref({})
    const userIamMenu = ref([])
    // app菜单
    const appMenu = ref([])
    // 顶部导航
    const defaultTopMenu = {
        label: '联合运营平台',
        path: '/joint',
        remark: '联合运营',
    }

    const setUserInfo = (info) => {
        userInfo.value = info
    }
    const getUserInfo = () => {
        return userInfo.value
    }

    const getUserToken = () => {
        return userInfo.value.iamToken
    }

    const setUserIamMenu = (menu) => {
        userIamMenu.value = menu
    }
    // 主菜单,由iam和app菜单拼接
    const userMenu = computed(() => {
        const roleRouters = listToTree(data.data.iamAppMenuVoList)
        console.log('🚀 ~ data-roleRouters:', roleRouters)
        return roleRouters
    })

    const topMenu = computed(() => {
        console.log('🚀 ~ topMenu ~ userMenu.value:', userMenu.value)
        const topApps = userMenu.value.map(item => ({
            label: `${item?.meta?.title}`,
            path: `/${item?.path}`,
            remark: `${item?.remark || item?.meta?.title}`,
        }))

        return topApps
    })

    const setAppRoles = (roles) => {
        appRoles.value = roles
    }

    const getUserMenu = computed(() => {
        return userMenu.value
    })
    const getAppMenu = computed(() => {
        return appMenu.value
    })

    return {
        userInfo,
        setUserInfo,
        getUserInfo,
        getUserToken,
        setUserIamMenu,
        getUserMenu,
        setAppRoles,
        getAppMenu,
        topMenu,
        userMenu,
    }
})
